﻿//********************************************************************************//
//创建日期：<创建日期，2016-10-09>
//创建作者：<孙雪婷，sunxueting@shanghai3h.com>
//功能说明：深井信息查询
//修改:郭登杰 at20170717
//********************************************************************************//

using System;
using System.Data;
using BLL;
using BLL.Access;
using DevExpress.Web;
using Entity;
using Web.Class;
using System.Text.RegularExpressions;

namespace Web.Pages.QuShui.DiXiaS.ChaXunGL
{
    public partial class ShenJingXXCX : BasePage
    {
        #region 属性

        /// <summary>
        ///     保存结果集1
        /// </summary>
        private DataTable dtShenJingXX
        {
            get { return ViewState["dtShenJingXX"] as DataTable; }
            set { ViewState["dtShenJingXX"] = value; }
        }

        #endregion

        protected void btnExport_Click(object sender, EventArgs e)
        {
            ASPxGridViewExporter1.GridViewID = "gvShenJingXX";
            var sName = "深井信息";
            gvShenJingXX.Settings.ShowFooter = false;
            for (var i = 0; i < dtShenJingXX.Rows.Count; i++)
            {
                var row = dtShenJingXX.Rows[i];
                for (var j = 0; j < dtShenJingXX.Columns.Count; j++)
                    if (row[j].GetType().Equals(typeof(string)) && Regex.IsMatch(row[j].ToString(), @"^[+-]?\d*$"))
                        row[j] = "\r" + row[j];
            }

            ASPxGridViewExporter1.WriteXlsToResponse(sName);
        }

        public class Conditions
        {
            public string ym { get; set; }
            public string huanxianbj { get; set; }
            public string xingzhengq { get; set; }
            public string ywy { get; set; }
            public string yhh { get; set; }
            public string yhm { get; set; }
            public string dizhi { get; set; }

            public string jinbh { get; set; }
            public string xkzh { get; set; }
            public string jindz { get; set; }

            public string qushuiyt { get; set; }
            public string hangyelx { get; set; }
            public string bumen { get; set; }

            public string jinlx { get; set; }
            public string jinxz { get; set; }
            public string jinbj { get; set; }

            public string laobh { get; set; }
        }

        #region 变量

        /// <summary>
        ///     数据访问操作类
        /// </summary>
        private SpHelper _SpHelper = new SpHelper();

        /// <summary>
        ///     行政区
        /// </summary>
        private QS_JX_XINGZHENGQBLL XZQ = new QS_JX_XINGZHENGQBLL();

        /// <summary>
        ///     行业分类
        /// </summary>
        private QS_JX_HANGYEFLXXBLL HYFL = new QS_JX_HANGYEFLXXBLL();

        /// <summary>
        ///     取水用途
        /// </summary>
        private QS_JX_QUSHUIXXBLL QSYT = new QS_JX_QUSHUIXXBLL();

        /// <summary>
        ///     管理部门
        /// </summary>
        private QS_JX_BUMENXXBLL GLBM = new QS_JX_BUMENXXBLL();

        /// <summary>
        ///     流域信息
        /// </summary>
        private QS_JX_LIUYUXXBLL LYXX = new QS_JX_LIUYUXXBLL();

        /// <summary>
        ///     水利分片
        /// </summary>
        private QS_JX_SHUILIFPXXBLL SLFP = new QS_JX_SHUILIFPXXBLL();

        /// <summary>
        ///     水资源分区
        /// </summary>
        private QS_JX_SHUIZIYFQBLL SZYFQ = new QS_JX_SHUIZIYFQBLL();

        /// <summary>
        ///     井标记
        /// </summary>
        private readonly QS_JX_JINGBIAOJXXBLL JBJ = new QS_JX_JINGBIAOJXXBLL();

        #endregion

        #region 事件

        /// <summary>
        ///     页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack) InitData();
            gvShenJingXX.DataSource = dtShenJingXX;
            gvShenJingXX.DataBind();
        }


        /// <summary>
        ///     查询信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnChaXun_Click(object sender, EventArgs e)
        {
            if (txtShuiLiangYM.Text != "")
            {
                LoadGridData(true);
                if (dtShenJingXX == null || dtShenJingXX.Rows.Count < 1)
                {
                    txtYongHuH2.Text = "";
                    txtYongHuM2.Text = "";
                    txtHuanNeiHW.Text = "";
                    txJingBianH.Text = "";
                    txtDiZhi.Text = "";
                    txtXingZhengQ.Text = "";
                    txtJingLeiX.Text = "";
                    txtJingXingZ.Text = "";
                    txtJingBiaoJ.Text = "";
                    txtLiuYu.Text = "";
                    txtYeWuY.Text = "";
                    txtGuanLiBM.Text = "";
                    txtShuiZiYFQ.Text = "";
                    txtCengCi.Text = "";
                    txtShenDu.Text = "";
                    txtQuShuiYT.Text = "";
                    txtShuiLiFP.Text = "";
                    txtKouJing.Text = "";
                    txtHangYeFL.Text = "";
                    memoBeiZhu.Text = "";
                    txtYuanJingH.Text = "";
                    txtShuiBiaoSL.Text = "";
                    txtXuKeZ.Text = "";
                    txtBenYueSL.Text = "";
                    zjl.Text = "共0条";
                }
            }
        }


        /// <summary>
        ///     tab1导出excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            if (dtShenJingXX == null || dtShenJingXX.Rows.Count < 1)
                PageTools.AlertMsg(this, "没有符合的数据导出");
            else
                PageTools.ExportExcel(dtShenJingXX);
        }

        #endregion

        #region 方法

        /// <summary>
        ///     初始化数据
        /// </summary>
        private void InitData()
        {
            var wtype = BusinessTool.WType;
            gvShenJingXX.SettingsPager.Mode = GridViewPagerMode.EndlessPaging;
            //水量年月
            var water_y_m = BusinessTool.GetSLYMorZWNY(1, wtype).ToString();

            var userid = LoginInfo.CurrentUser.UserID; //当前登陆人
            if (userid == 116 || userid == 99999) btnSLPC.Visible = true;

            if (userid == 116 || userid == 99999 || userid == 117 || userid == 111) btnXiuGai.Visible = true;

            //业务员
            //LoadYWY();
            txtShuiLiangYM.Text = water_y_m;
            //加载管理部门
            LoadGLBM();
            //加载行政区
            LoadXZQ();
            //加载业务员
            LoadYWY();
            //加载取水用途
            LoadQSYT();
            //加载行业分类
            LoadHYFL();
            //加载井标记
            LoadLY();
        }


        /// <summary>
        ///     加载管理部门
        /// </summary>
        private void LoadGLBM()
        {
            PageTools.QSLoadCombox(cbmGuanLiBM, PageTools.eLoadType.管理部门QS, null, "");
        }

        /// <summary>
        ///     加载行政区
        /// </summary>
        private void LoadXZQ()
        {
            PageTools.QSLoadCombox(cmbXingZhengQ, PageTools.eLoadType.行政区QS, null, "");
        }

        /// <summary>
        ///     加载业务员
        /// </summary>
        private void LoadYWY()
        {
            PageTools.QSLoadCombox(cmbYeWuY, PageTools.eLoadType.业务员QS, null, "");
        }


        /// <summary>
        ///     加载 取水用途
        /// </summary>
        private void LoadQSYT()
        {
            PageTools.QSLoadCombox(cmbQuShuiYT, PageTools.eLoadType.取水用途QS, null, "");
        }

        /// <summary>
        ///     加载行业分类
        /// </summary>
        private void LoadHYFL()
        {
            PageTools.QSLoadCombox(cmbHangYeLX, PageTools.eLoadType.行业分类QS, null, "");
        }

        /// <summary>
        ///     加载井标记
        /// </summary>
        private void LoadLY()
        {
            var dt = JBJ.SelectQS_JX_JINGBIAOJXXesDynamic("I_JLZT=0", null);
            for (var i = 0; i < dt.Rows.Count; i++)
                cmbJingBiaoJ.Items.Add(dt.Rows[i]["S_YUZHIMC"].ToString(), dt.Rows[i]["S_YUZHIBH"]);
        }

        private Conditions getConditions()
        {
            var conditions = new Conditions();
            conditions.ym = string.IsNullOrEmpty(txtShuiLiangYM.Text) ? null : txtShuiLiangYM.Text;
            conditions.huanxianbj = cmbHuanNeiHW.SelectedIndex <= 0 ? null : cmbHuanNeiHW.Value.ToString().Trim();
            conditions.xingzhengq = cmbXingZhengQ.SelectedIndex <= 0 ? null : Convert.ToString(cmbXingZhengQ.Value);
            conditions.ywy = cmbYeWuY.SelectedIndex <= 0 ? null : Convert.ToString(cmbYeWuY.Value);

            conditions.laobh = string.IsNullOrEmpty(txtYjingH.Text) ? null : txtYjingH.Text;
            conditions.jinlx = cmbJingLeiX.SelectedIndex <= 0 ? null : Convert.ToString(cmbJingLeiX.Value);
            conditions.jinxz = cmbJingZhuangT.SelectedIndex <= 0 ? null : Convert.ToString(cmbJingZhuangT.Value);
            conditions.jinbj = cmbJingBiaoJ.SelectedIndex <= 0 ? null : Convert.ToString(cmbJingBiaoJ.Value);

            conditions.yhh = string.IsNullOrEmpty(txtYongHuH.Text) ? null : txtYongHuH.Text;
            conditions.yhm = string.IsNullOrEmpty(txtYongHuM.Text) ? null : txtYongHuM.Text;
            conditions.dizhi = string.IsNullOrEmpty(txtYongHuDZ.Text) ? null : txtYongHuDZ.Text;
            conditions.jinbh = string.IsNullOrEmpty(txtJingBianH.Text) ? null : txtJingBianH.Text;
            conditions.xkzh = string.IsNullOrEmpty(txtXuKeZH.Text) ? null : txtXuKeZH.Text;
            conditions.jindz = string.IsNullOrEmpty(txtQuShuiDZ.Text) ? null : txtQuShuiDZ.Text;
            conditions.qushuiyt = cmbQuShuiYT.SelectedIndex <= 0 ? null : cmbQuShuiYT.Value.ToString();
            conditions.hangyelx = cmbHangYeLX.SelectedIndex <= 0 ? null : cmbHangYeLX.Value.ToString();
            conditions.bumen = cbmGuanLiBM.SelectedIndex <= 0 ? null : cbmGuanLiBM.Value.ToString();

            return conditions;
        }

        /// <summary>
        ///     加载查询数据
        /// </summary>
        private DataTable LoadGridData(bool reget)
        {
            var cs = getConditions();
            if (reget)
                dtShenJingXX = new SpHelper().P_QS_CX_SHENJINXXCX(null, int.Parse(cs.ym) / 100,
                    int.Parse(cs.ym) % 100,
                    cs.huanxianbj,
                    cs.xingzhengq,
                    cs.laobh,
                    cs.jinlx,
                    cs.jinxz,
                    cs.jinbj,
                    null,
                    null,
                    null,
                    cs.yhh,
                    cs.yhm,
                    cs.dizhi,
                    cs.jinbh,
                    cs.xkzh,
                    cs.jindz,
                    cs.qushuiyt,
                    cs.hangyelx,
                    cs.bumen,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    cs.ywy
                ).Tables[0];
            gvShenJingXX.DataSource = dtShenJingXX;
            gvShenJingXX.DataBind();
            if (dtShenJingXX != null && dtShenJingXX.Rows.Count > 0)
            {
                zjl.Text = "共" + dtShenJingXX.Rows.Count + "条记录";
                gvShenJingXX.FocusedRowIndex = -1;
                gvShenJingXX.FocusedRowIndex = 0;
            }
            else
            {
                zjl.Text = "共0条记录";
            }

            return dtShenJingXX;
            /*
            int ym = Convert.ToInt32(txtShuiLiangYM.Text);
            int hnhw = cmbHuanNeiHW.SelectedIndex<=0?0: Convert.ToInt32(cmbHuanNeiHW.Value);
            string xzq = cmbXingZhengQ.Text != "全部" ? cmbXingZhengQ.Value.ToString() : null;
            string ywy = cmbYeWuY.Value != null ? cmbYeWuY.Value.ToString() : "0";
            int v_bs = -1;
            string v_val = null;
            //1 用户号，2 用户名，3 用户地址，4 取水用途，5 行业类型 ，6 管理部门 ，
            //7 井编号 ，8 取水地址（井地址），9 许可证号，10 井类型，11 井状态，12 井标记，13 原井号，
            if (chkYongHuH.Checked && txtYongHuH.Text != "")
            {
                v_bs = 1;
                v_val = txtYongHuH.Text;
            }
            else if (chkYongHuM.Checked && txtYongHuM.Text != "")
            {
                v_bs = 2;
                v_val = txtYongHuM.Text;
            }
            else if (chkYongHuDZ.Checked && txtYongHuDZ.Text != "")
            {
                v_bs = 3;
                v_val = txtYongHuDZ.Text;
            }
            else if (rabQuShuiYT.Checked && cmbQuShuiYT.Text != "")
            {
                v_bs = 4;
                v_val = cmbQuShuiYT.Value.ToString();
            }
            else if (rabHangYeLX.Checked && cmbHangYeLX.Text != "")
            {
                v_bs = 5;
                v_val = cmbHangYeLX.Value.ToString();
            }
            else if (rabGuanLiBM.Checked && cbmGuanLiBM.Text != "")
            {
                v_bs = 6;
                v_val = cbmGuanLiBM.Value.ToString();
            }
            else if (rabJingBianH.Checked && txtJingBianH.Text != "")
            {
                v_bs = 7;
                v_val = txtJingBianH.Text;
            }
            else if (rabQuShuiDZ.Checked && txtQuShuiDZ.Text != "")
            {
                v_bs = 8;
                v_val = txtQuShuiDZ.Text;
            }
            else if (rabXuKeZH.Checked && txtXuKeZH.Text != "")
            {
                v_bs = 9;
                v_val = txtXuKeZH.Text;
            }
            else if (rabJingLeiX.Checked && cmbJingLeiX.Text != "")
            {
                v_bs = 10;
                v_val = cmbJingLeiX.Value.ToString();
            }
            else if (rabJingZhuangT.Checked && cmbJingZhuangT.Text != "")
            {
                v_bs = 11;
                v_val = cmbJingZhuangT.Value.ToString();
            }
            else if (rabJingBiaoJ.Checked && cmbJingBiaoJ.Text != "")
            {
                v_bs = 12;
                v_val = cmbJingBiaoJ.Value.ToString();
            }
            else if (rabYjingH.Checked && txtYjingH.Text != "")
            {
                v_bs = 13;
                v_val = txtYjingH.Text;
            }
            this.dtShenJingXX = _SpHelper.P_QSCX_SHENJINGXXCX(BusinessTool.WType, ym, hnhw, xzq, ywy, v_bs, v_val, 0).Tables[0];
            gvShenJingXX.DataSource = this.dtShenJingXX;
            gvShenJingXX.DataBind();
            if (this.dtShenJingXX != null && this.dtShenJingXX.Rows.Count > 0)
            {
                zjl.Text = "共" + this.dtShenJingXX.Rows.Count.ToString() + "条";
                gvShenJingXX.FocusedRowIndex = 0;
                SetData();
            }
            */
        }

        /// <summary>
        ///     给显示框赋值
        /// </summary>
        /// <summary>
        ///     前台表格行改变事件回调方法
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void ASPxCallback1_Callback(object source, CallbackEventArgs e)
        {
            var id = Convert.ToInt32(hdCollection.Get("ID"));
            if (e.Parameter == "setinfo")
            {
                var msg = "";
                if (dtShenJingXX != null)
                {
                    var cs = getConditions();
                    var dt_temp = new SpHelper().P_QS_CX_SHENJINXXCX(id.ToString(), int.Parse(cs.ym) / 100,
                        int.Parse(cs.ym) % 100,
                        cs.huanxianbj,
                        cs.xingzhengq,
                        cs.laobh,
                        cs.jinlx,
                        cs.jinxz,
                        cs.jinbj,
                        null,
                        null,
                        null,
                        cs.yhh,
                        cs.yhm,
                        cs.dizhi,
                        cs.jinbh,
                        cs.xkzh,
                        cs.jindz,
                        cs.qushuiyt,
                        cs.hangyelx,
                        cs.bumen,
                        null,
                        null,
                        null,
                        null,
                        null,
                        null,
                        null,
                        null,
                        null,
                        null,
                        cs.ywy
                    ).Tables[0];
                    //DataTable dt_temp = _SpHelper.P_QSCX_SHENJINGXXCX(2, 0, 0, "0", "0", -1, null, id).Tables[0];

                    msg = string.Join("|",
                        dt_temp.Rows[0]["S_YONGHUH"].ToString(),
                        dt_temp.Rows[0]["S_YONGHUM"].ToString(),
                        dt_temp.Rows[0]["HUANXIANBJ"].ToString(),
                        dt_temp.Rows[0]["S_JINGBH"].ToString(),
                        dt_temp.Rows[0]["S_DIZHI"].ToString(),
                        dt_temp.Rows[0]["S_XINGZHENGQM"].ToString(),
                        dt_temp.Rows[0]["JINLX"].ToString(),
                        dt_temp.Rows[0]["JINXZ"].ToString(),
                        dt_temp.Rows[0]["JINBJ"].ToString(),
                        dt_temp.Rows[0]["S_LIUYUMC"].ToString(),
                        dt_temp.Rows[0]["S_YEWUY"].ToString(),
                        dt_temp.Rows[0]["S_BUMENMC"].ToString(),
                        dt_temp.Rows[0]["S_SHUIZIYMC"].ToString(),
                        dt_temp.Rows[0]["I_CENG"].ToString(),
                        dt_temp.Rows[0]["S_SHENGDU"].ToString(),
                        dt_temp.Rows[0]["S_QUSHUIMC"].ToString(),
                        dt_temp.Rows[0]["S_SHUILIFPMC"].ToString(),
                        dt_temp.Rows[0]["I_KONGJING"].ToString(),
                        dt_temp.Rows[0]["S_FENLEIMC"].ToString(),
                        dt_temp.Rows[0]["S_BEIZHU"].ToString(),
                        dt_temp.Rows[0]["S_LAOBIANH"].ToString(),
                        dt_temp.Rows[0]["BIAOSHU"].ToString(),
                        dt_temp.Rows[0]["S_XUKEZHENGBH"].ToString());
                }

                e.Result = e.Parameter + "|0|" + msg;
            }
        }

        #endregion
    }
}